<template>
  <div class="patient-appointments">
    <div class="appointments-header">
      <h2>预约记录</h2>
      <el-button type="primary" icon="Plus" @click="createNewAppointment">新建预约</el-button>
    </div>
    
    <el-card class="appointments-card">
      <el-table :data="appointments" style="width: 100%" stripe>
        <el-table-column prop="date" label="日期" width="150" sortable />
        <el-table-column prop="time" label="时间" width="100" />
        <el-table-column prop="hospital" label="医院" width="180" />
        <el-table-column prop="department" label="科室" width="120" />
        <el-table-column prop="doctor" label="医生" width="120" />
        <el-table-column label="状态" width="120">
          <template #default="scope">
            <el-tag :type="statusTagType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button 
              type="primary" 
              size="small"
              @click="editAppointment(scope.row)"
              :disabled="scope.row.status === '已完成'"
            >
              修改
            </el-button>
            <el-button 
              type="danger" 
              size="small"
              @click="cancelAppointment(scope.row)"
              :disabled="scope.row.status === '已完成' || scope.row.status === '已取消'"
            >
              取消
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes, total"
          :total="total"
          background
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const appointments = ref([
  { id: 1, date: '2023-07-28', time: '10:30', hospital: '市中心医院', department: '内科', doctor: '张医生', status: '已确认' },
  { id: 2, date: '2023-08-05', time: '14:00', hospital: '人民医院', department: '眼科', doctor: '李医生', status: '待确认' },
  { id: 3, date: '2023-07-15', time: '09:45', hospital: '中医院', department: '骨科', doctor: '王医生', status: '已完成' },
  { id: 4, date: '2023-06-20', time: '11:15', hospital: '儿童医院', department: '儿科', doctor: '刘医生', status: '已取消' },
])

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(4)

const statusTagType = (status) => {
  const statusMap = {
    '已确认': 'success',
    '待确认': 'warning',
    '已完成': 'info',
    '已取消': 'danger'
  }
  return statusMap[status] || ''
}

const createNewAppointment = () => {
  ElMessage.info('创建新预约功能')
}

const editAppointment = (appointment) => {
  ElMessage.info(`修改预约: ${appointment.id}`)
}

const cancelAppointment = (appointment) => {
  ElMessageBox.confirm('确定要取消此预约吗？', '取消预约', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 更新状态为已取消
    const index = appointments.value.findIndex(a => a.id === appointment.id)
    if (index !== -1) {
      appointments.value[index].status = '已取消'
    }
    ElMessage.success('预约已取消')
  }).catch(() => {
    // 用户取消操作
  })
}
</script>

<style scoped>
.patient-appointments {
  padding: 20px;
}

.appointments-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.appointments-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>